# -*- coding: utf-8 -*-
"""
作者: liuzaiqiang
邮箱: zaiqiangliu@163.com
地址: 新疆大学(博达校区)
职业：学生(2025级研一)
创建时间: 2025/11/29 23:23
"""


# coding=utf-8
import sys


def main():
    n, k = map(int, sys.stdin.readline().split())
    w = list(map(int, sys.stdin.readline().split()))

    if n == k:
        print(0)
        return

    # 预处理前缀最大值和最小值
    prefix_max = [0] * n
    prefix_min = [0] * n
    prefix_max[0] = prefix_min[0] = w[0]
    for i in range(1, n):
        prefix_max[i] = max(prefix_max[i - 1], w[i])
        prefix_min[i] = min(prefix_min[i - 1], w[i])

    # 预处理后缀最大值和最小值
    suffix_max = [0] * n
    suffix_min = [0] * n
    suffix_max[-1] = suffix_min[-1] = w[-1]
    for i in range(n - 2, -1, -1):
        suffix_max[i] = max(suffix_max[i + 1], w[i])
        suffix_min[i] = min(suffix_min[i + 1], w[i])

    min_stability = float('inf')

    # 枚举所有可能的未探测区域
    for i in range(n - k + 1):
        left_max = prefix_max[i - 1] if i > 0 else -float('inf')
        left_min = prefix_min[i - 1] if i > 0 else float('inf')
        right_max = suffix_max[i + k] if (i + k) < n else -float('inf')
        right_min = suffix_min[i + k] if (i + k) < n else float('inf')

        current_max = max(left_max, right_max)
        current_min = min(left_min, right_min)

        min_stability = min(min_stability, current_max - current_min)

    print(min_stability)


if __name__ == "__main__":
    main()